package br.megareleased;

import java.io.InputStreamReader;
import java.io.Reader;
import java.net.URL;

import javax.swing.text.MutableAttributeSet;
import javax.swing.text.html.HTML;
import javax.swing.text.html.HTMLEditorKit.ParserCallback;
import javax.swing.text.html.parser.ParserDelegator;

public class GetLink {
	
	private final String url;
	private String link = null;

	public GetLink(String url) {
		this.url = url;
	}
	
	public String getMegaUploadLink() {
		try {
			URL url = new URL(this.url);
			Reader reader = new InputStreamReader(url.openStream());
			ParserDelegator pp = new ParserDelegator();
			pp.parse(reader, new ParserCallback() {
				public void handleStartTag(HTML.Tag t, MutableAttributeSet a, int pos) {
					if (t == HTML.Tag.A) {
						if(a.getAttribute(HTML.Attribute.HREF) != null && a.getAttribute(HTML.Attribute.HREF).toString().toLowerCase().contains("megaupload.com")) {
						    link = a.getAttribute(HTML.Attribute.HREF).toString();
						}
					}
				}
			}, true);
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return link;
	}
}
